package com.yisoo.ubag.iot.service.impl;

import com.yisoo.common.to.RefreshBagStatusTo;
import com.yisoo.common.utils.MyBeanUtils;
import com.yisoo.common.utils.R;
import com.yisoo.ubag.iot.entity.DeviceEntity;
import com.yisoo.ubag.iot.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.iot.dao.StatusDao;
import com.yisoo.ubag.iot.entity.StatusEntity;
import com.yisoo.ubag.iot.service.StatusService;


@Service("statusService")
public class StatusServiceImpl extends ServiceImpl<StatusDao, StatusEntity> implements StatusService {

    @Autowired
    DeviceService deviceService;
    @Autowired

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StatusEntity> page = this.page(
                new Query<StatusEntity>().getPage(params),
                new QueryWrapper<StatusEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveOrUpdateByDeviceNameAndMsg(StatusEntity content) {
        String deviceName = content.getDeviceName();
        String msg = content.getMsg();

        QueryWrapper<StatusEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_name",deviceName);
        queryWrapper.eq("msg",msg);
        List<StatusEntity> statusEntities = baseMapper.selectList(queryWrapper);
        if(statusEntities == null || statusEntities.size() == 0){
            this.save(content);
        }else{
            content.setStatusId(statusEntities.get(0).getStatusId());
            this.updateById(content);
        }
    }

    @Override
    public RefreshBagStatusTo getBagUpStatus(Long bagId) {
        DeviceEntity deviceEntity = deviceService.getById(bagId);
        QueryWrapper<StatusEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_name",deviceEntity.getDeviceName());
        StatusEntity statusEntity = baseMapper.selectOne(queryWrapper);
        if (statusEntity == null){
            return  new RefreshBagStatusTo();
        }else {
            RefreshBagStatusTo refreshBagStatusTo = new RefreshBagStatusTo();
            MyBeanUtils.copyProperties(statusEntity, refreshBagStatusTo);
            return refreshBagStatusTo;
        }
    }

    @Override
    public R getStatusByDevName(String devName) {

        QueryWrapper<StatusEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_name",devName);
        StatusEntity statusEntity = baseMapper.selectOne(queryWrapper);
        if (statusEntity == null){
            return R.error("设备未找到");
        }else{
            double xData = statusEntity.getDisXData();
            double yData = statusEntity.getDisYData();
            int tempData = 0;
            tempData = ((int)xData)/100;
//            System.out.println(xData+"-"+tempData);

            double lat = tempData+(xData-tempData*100)/60.00;
            tempData = ((int)yData)/100;
//            System.out.println(yData+"-"+tempData);
            double lng = tempData+(yData-tempData*100)/60.00;
            return R.ok().put("data",statusEntity).put("lng",lng).put("lat",lat);
        }
    }

}